﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BiblioWeb.DataAccess;
using BiblioWebProyecto2013.Models;

namespace BiblioWebProyecto2013.Controllers
{
    public class EditorialController : Controller
    {
        private ContextoBiblio db;
        private EditorialRepo editorialRepo;

        public EditorialController()
        {
            this.db = new ContextoBiblio();
            this.editorialRepo = new EditorialRepo(db);
        }

        //
        // GET: /Editorial/

        public ActionResult Index()
        {
            return View(this.editorialRepo.Todos());
        }

        //
        // GET: /Editorial/Details/5

        public ActionResult Details(int id = 0)
        {
            Editorial editorial = this.editorialRepo.Obtener(id);
            if (editorial == null)
            {
                return HttpNotFound();
            }
            return View(editorial);
        }

        //
        // GET: /Editorial/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Editorial/Create

        [HttpPost]
        public ActionResult Create(Editorial editorial)
        {
            if (ModelState.IsValid)
            {
                this.editorialRepo.Agregar(editorial);
                return RedirectToAction("Index");
            }

            return View(editorial);
        }

        //
        // GET: /Editorial/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Editorial editorial = this.editorialRepo.Obtener(id);
            if (editorial == null)
            {
                return HttpNotFound();
            }
            return View(editorial);
        }

        //
        // POST: /Editorial/Edit/5

        [HttpPost]
        public ActionResult Edit(Editorial editorialModificada)
        {
            if (ModelState.IsValid)
            {
                Editorial editorialActual = this.editorialRepo.Obtener(editorialModificada.IdEditorial);

                editorialActual.NombreEditorial = editorialModificada.NombreEditorial;

                this.editorialRepo.Modificar(editorialActual);
                return RedirectToAction("Index");
            }
            return View(editorialModificada);
        }

        //
        // GET: /Editorial/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Editorial editorial = this.editorialRepo.Obtener(id);
            if (editorial == null)
            {
                return HttpNotFound();
            }
            return View(editorial);
        }

        //
        // POST: /Editorial/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            this.editorialRepo.Eliminar(id);
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}